home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-20 | 13.7 KB | 557 lines | [TEXT/MPS ] |
- // Copyright © 1995 by Apple Computer, Inc. All rights reserved.
- // PCISlotsUtils.cp
-
- /*
- FILE
- PCISlotsUtils.cp
-
- NAME
- PCI Slot Peek utility source file
-
- DESCRIPTION
- This source file implements misc. utility functions used by the
- PCI Slot Peek application. It provides the 68K->PPC glue code
- to the NameRegistry library.
-
- MODIFICATION HISTORY
- Created by Terry Teague
-
- 15 May 95 - TRT - Initial version
- 17 Jul 95 - TRT - Tidy-up source for PPCC v1.0.5, Universal Headers v2.0 final,
- CodeWarrior 6.1. Add support for finding slot # given driver refNum.
-
- =============================================================================================
- */
-
- //================================================================================================
- // Build-time #defines
- //================================================================================================
-
- // pre-Universal Headers (<MPW 3.3.1) compatibility
- #ifndef qPreUniHeaders
- #define qPreUniHeaders 0
- #endif
-
- //================================================================================================
- // Standard Includes
- //================================================================================================
-
- #include <Types.h>
- #include <Packages.h>
- #include <Dialogs.h>
- #include <Memory.h>
- //#if qPreUniHeaders
- //#include <OSEvents.h>
- //#endif
- #include <QuickDraw.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <StdIO.h>
- #include <Slots.h>
- #include <ROMDefs.h>
- #include <Devices.h>
- #include <Traps.h>
- #include <String.h>
- #include <Strings.h>
- #include <CType.h>
- #if qPreUniHeaders
- #include <SysEqu.h>
- #else
- #include <LowMem.h>
- #endif
- #include <Power.h>
- #include <DeskBus.h>
-
- // v2.0a3 or earlier
- //#ifndef __GESTALTEQU__
- //#include <GestaltEqu.h>
- //#endif
-
- // v2.0 final or later
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- // v2.0 final or later
- #ifndef __CODEFRAGMENTS__
- #include <CodeFragments.h>
- #endif
-
- // v2.0a3 or earlier (also kept in v2.0 final for compatibility)
- //#ifndef __FRAGLOAD__
- //#include <FragLoad.h>
- //#endif
-
- #ifndef __MIXEDMODE__
- #include <MixedMode.h>
- #endif
-
- // v2.0a3 or earlier
- #ifdef __FRAGLOAD__
- typedef SInt32 OSStatus;
- #endif
-
- #ifndef __NAMEREGISTRY__
- #include <NameRegistry.h>
- #endif
-
- //================================================================================================
- // PCISlots Specific Includes
- //================================================================================================
-
- #ifndef __PCISLOTS__
- #include "PCISlots.h"
- #endif
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
- #pragma options align=mac68k
- #endif
-
- #ifdef __CFM68K__
- #pragma lib_export on
- #endif
-
- Boolean FormatStringProperty(
- RegPropertyValueSize propertySize,
- const void *propertyValue,
- char *formatStr,
- char *result)
- {
- Boolean isStringProperty;
- Boolean isAscii;
- int i;
- register char *cp;
-
- isStringProperty = FALSE;
-
- if (propertySize < (kOneLineFormatLength - 3))
- {
- isAscii = TRUE;
- cp = ((char *) propertyValue);
- for (i = 0; isAscii && i < (propertySize - 1); i++)
- isAscii = IS_ASCII_PRINT(cp[i]);
- if (isAscii && cp[i] == '\0')
- {
- //sprintf(result, "\"%s\"", cp);
- sprintf(result, formatStr, cp);
- isStringProperty = TRUE;
- }
- }
-
- return (isStringProperty);
- }
-
- Boolean FormatSmallProperty(
- RegPropertyValueSize propertySize,
- const void *propertyValue,
- char *formatStr,
- char *result)
- {
- Boolean isSmallProperty;
- long *lp;
- short *sp;
-
- isSmallProperty = FALSE;
-
- if (propertySize == sizeof (short))
- {
- sp = (short *) propertyValue;
- //sprintf(result, "[%04x = %d]", ((int) *sp) & 0xFFFF, (int) *sp);
- sprintf(result, formatStr, ((int) *sp) & 0xFFFF);
- isSmallProperty = TRUE;
- }
-
- if (propertySize == sizeof (long))
- {
- lp = (long *) propertyValue;
- //sprintf(result, "[%08x = %ld]", lp[0], lp[0]);
- sprintf(result, formatStr, lp[0]);
- isSmallProperty = TRUE;
- } else
-
- if (propertySize == (sizeof (long) * 2))
- {
- lp = (long *) propertyValue;
- //sprintf(result, "[%08x = %ld], [%08x = %ld]", lp[0], lp[0], lp[1], lp[1]);
- sprintf(result, formatStr, lp[0], lp[1]);
- isSmallProperty = TRUE;
- }
-
- return (isSmallProperty);
- }
-
- /*
- * EnumeratePropertiesForThisName finds selected properties for the current path,
- * based on info stored in a STR# resource, and then displays those properties.
- */
- void EnumeratePropertiesForThisName(
- RegEntryID *entryID,
- const RegCStrPathName *pathName)
- {
- OSStatus status;
- Boolean done;
- RegPropertyNameBuf foundProperty;
- RegPropertyValueSize propertySize;
- // RegPropertyModifiers propertyModifiers;
- void *propertyValue;
- short whichProperty;
-
- done = FALSE;
-
- for (whichProperty = 1; !done; whichProperty++)
- {
- // get the next property that we are interested in
-
- getindstring((char *)foundProperty, kPropertyNameSTRID, whichProperty);
- done = foundProperty[0] == '\0'; // no more properties found in STR# resource
-
- if (!done)
- {
- propertyValue = NULL;
- propertySize = 0;
- status = myRegistryPropertyGetSize(entryID, foundProperty, &propertySize);
-
- if (status == noErr)
- {
- propertyValue = NewPtr(propertySize);
-
- if (propertyValue == NULL)
- {
- status = MemError();
- //DebugStr((const unsigned char*)"\pNo memory to store property");
- }
- }
-
- if (status == noErr)
- {
- status = myRegistryPropertyGet(entryID, foundProperty, propertyValue, &propertySize);
- }
-
- #if 0
- if (status == noErr)
- {
- status = myRegistryPropertyGetMod(entryID, foundProperty, &propertyModifiers);
- }
- #endif
-
- if (status == noErr)
- {
- char tempstr[256],
- tempstr1[256];
-
- getindstring(tempstr, kDisplayPropertyValueSTRID, whichProperty);
-
- if (!FormatStringProperty(propertySize, propertyValue, tempstr, tempstr1))
- FormatSmallProperty(propertySize, propertyValue, tempstr, tempstr1);
-
- //sprintf(tempstr2, tempstr, tempstr1); // formatting string
- printf("%s\n", tempstr1);
- }
-
- if (propertyValue != NULL)
- DisposePtr((Ptr) propertyValue);
- }
- }
-
- printf("\n");
- }
-
- /*
- * GetSlotForThisName returns the "AAPL,slot-name" property (if valid) for the current path.
- */
- OSErr GetSlotForThisName(
- RegEntryID *entryID,
- const RegCStrPathName *pathName)
- {
- OSStatus status;
- RegPropertyNameBuf foundProperty;
- RegPropertyValueSize propertySize;
- void *propertyValue;
-
- propertyValue = NULL;
- propertySize = 0;
- getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotPropertyName);
- //strcpy(foundProperty, "AAPL,slot-name");
-
- status = myRegistryPropertyGetSize(entryID, foundProperty, &propertySize);
-
- if (status == noErr)
- {
- propertyValue = NewPtr(propertySize);
-
- if (propertyValue == NULL)
- {
- status = MemError();
- //DebugStr((const unsigned char*)"\pNo memory to store property");
- }
- }
-
- if (status == noErr)
- {
- status = myRegistryPropertyGet(entryID, foundProperty, propertyValue, &propertySize);
-
- // Slot names on Power Macintosh currently are of form : A1, B1, C1, D2, E2, F2
- // but older versions of drivers create slot-names like "mace" which are not what we are looking for
-
- if ((status == noErr) && (propertySize > 4)) // could check if slot name contains non-Hex digits
- status = -1; // doesn't appear to be a valid slot name
- }
-
- if (status == noErr)
- {
- char tempstr[256];
-
- getindstring(tempstr, kSlotPropertySTRID, kSlotPropertyValue); // formatting string
- printf(tempstr, (char *)propertyValue, pathName);
- printf("\n");
- }
-
- if (propertyValue != NULL)
- DisposePtr((Ptr) propertyValue);
-
- return (status);
- }
-
- /*
- * This is adapted from the sample in the 8/3/94 PCI Drivers draft. Enumerate all
- * paths and all properties for each path.
- */
- short EnumerateNameRegistry(void)
- {
- OSStatus status;
- RegEntryIter cookie;
- Boolean done;
- RegEntryIterationOp op;
- RegEntryID entry;
- RegCStrPathName *pathName;
- RegPathNameSize pathNameSize;
- short numSlots;
-
- numSlots = 0;
- op = kRegIterContinue;
- status = myRegistryEntryIterateCreate(&cookie);
-
- if (status == noErr)
- {
- done = FALSE;
-
- while (status == noErr && done == FALSE)
- {
- status = myRegistryEntryIterate(&cookie, op, &entry, &done);
-
- if (status == noErr && done == FALSE)
- {
- pathName = NULL;
- status = myRegistryEntryToPathSize(&entry, &pathNameSize);
-
- if (status == noErr)
- {
- pathName = (RegCStrPathName *) NewPtr(pathNameSize);
- if (pathName == NULL)
- status = MemError();
- }
-
- if (status == noErr)
- {
- status = myRegistryCStrEntryToPath(&entry, pathName, pathNameSize);
- //printf("%s\n", pathName); // debugging
- }
-
- if (status == noErr)
- {
- status = GetSlotForThisName(&entry, pathName);
-
- if (!status)
- {
- // Found a card in a slot
-
- numSlots++;
- EnumeratePropertiesForThisName(&entry, pathName);
-
- } else
- status = noErr; // at present
- }
-
- if (pathName != NULL)
- DisposePtr((Ptr) pathName);
-
- myRegistryEntryIDDispose(&entry);
- }
- }
-
- myRegistryEntryIterateDispose(&cookie);
- }
-
- return (numSlots);
- }
-
- //================================================================================================
- // Get PCI slot # for specified driver refNum, by searching NameRegistry for "driver-ref" properties.
- // assume slot #'s are of form $Xn (X = A, B, C, D, E, F; n = 1, 2), so we can return a "short"
- //================================================================================================
- short GetSlotForThisRefNum(short refNum)
- {
- short slotNum;
-
- OSStatus status;
- RegEntryIter cookie;
- Boolean done;
- RegEntryIterationOp op;
- RegEntryID entry;
- RegCStrPathName *pathName;
- RegPathNameSize pathNameSize;
- RegPropertyNameBuf foundProperty;
- RegPropertyValueSize propertySize;
- // RegPropertyModifiers propertyModifiers;
- void *propertyValue;
-
- slotNum = 0;
-
- op = kRegIterContinue;
- status = myRegistryEntryIterateCreate(&cookie);
-
- if (status == noErr)
- {
- done = FALSE;
-
- while (status == noErr && done == FALSE)
- {
- status = myRegistryEntryIterate(&cookie, op, &entry, &done);
-
- if (status == noErr && done == FALSE)
- {
- pathName = NULL;
- status = myRegistryEntryToPathSize(&entry, &pathNameSize);
-
- if (status == noErr)
- {
- pathName = (RegCStrPathName *) NewPtr(pathNameSize);
-
- if (pathName == NULL)
- status = MemError();
- }
-
- if (status == noErr)
- {
- status = myRegistryCStrEntryToPath(&entry, pathName, pathNameSize);
- //printf("%s\n", pathName); // debugging
- }
-
- if (status == noErr)
- {
- propertyValue = NULL;
- propertySize = 0;
- getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotPropertyName);
- //strcpy(foundProperty, "AAPL,slot-name");
-
- status = myRegistryPropertyGetSize(&entry, foundProperty, &propertySize);
-
- if (status == noErr)
- {
- propertyValue = NewPtr(propertySize);
-
- if (propertyValue == NULL)
- {
- status = MemError();
- //DebugStr((const unsigned char*)"\pNo memory to store property");
- }
- }
-
- if (status == noErr)
- {
- status = myRegistryPropertyGet(&entry, foundProperty, propertyValue, &propertySize);
-
- // Slot names on Power Macintosh currently are of form : A1, B1, C1, D2, E2, F2
- // but older versions of drivers create slot-names like "mace" which are not what we are looking for
-
- if ((status == noErr) && (propertySize > 4)) // could check if slot name contains non-Hex digits
- status = -1; // doesn't appear to be a valid slot name
- }
-
- if (status == noErr)
- {
- //sprintf(tempstr1, tempstr2, (char *)propertyValue, pathName);
- //sprintf(tempstr1, tempstr2, (char *)propertyValue);
- slotNum = *(short *)propertyValue; // convert it to a short
- }
-
- if (propertyValue != NULL)
- DisposePtr((Ptr) propertyValue);
-
- if (!status)
- {
- // Found a card in a slot
-
- getindstring((char *)foundProperty, kSlotPropertySTRID, kSlotDriverRefNumItem);
-
- propertyValue = NULL;
- propertySize = 0;
- status = myRegistryPropertyGetSize(&entry, foundProperty, &propertySize);
-
- if (status == noErr)
- {
- propertyValue = NewPtr(propertySize);
- if (propertyValue == NULL)
- {
- status = MemError();
- //DebugStr((const unsigned char*)"\pNo memory to store property");
- }
-
- if (status == noErr)
- {
- status = myRegistryPropertyGet(&entry, foundProperty, propertyValue, &propertySize);
- }
-
- #if 0
- if (status == noErr)
- {
- status = myRegistryPropertyGetMod(&entry, foundProperty, &propertyModifiers);
- }
- #endif
- if (status == noErr)
- {
- if (*(short *)propertyValue == refNum)
- {
- // we have found the correct driver, so we can stop
- done = TRUE;
- } else
- slotNum = 0; // reset it for the next slot
- }
-
- if (propertyValue != NULL)
- DisposePtr((Ptr) propertyValue);
- } else
- status = noErr; // there is a card but NO display attached (no open driver)
- } else
- status = noErr; // not a slot device
- }
-
- if (pathName != NULL)
- DisposePtr((Ptr) pathName);
-
- myRegistryEntryIDDispose(&entry);
- }
- }
-
- myRegistryEntryIterateDispose(&cookie);
- }
-
- return (slotNum);
- } /* getslotforthisrefnum */
-
- #ifdef __CFM68K__
- #pragma lib_export off
- #endif
-
- #if GENERATINGPOWERPC || defined(powerc) || defined (__powerc)
- #pragma options align=reset
- #endif
-
- #ifdef __cplusplus
- }
- #endif
-